import 'package:base_widgets/export.dart';
import 'package:flutter/material.dart';
import 'package:flutter_teach/constant/app_router.dart';
import '../bean/HomeArticleBean.dart';
import '/common/common_util.dart';
import '/web/page_web.dart';
import 'home_change_notifier.dart';

class HomePage extends StatelessWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<HomeChangeNotifier>(
      create: (BuildContext context) => HomeChangeNotifier(),
      child: PagingListWidget<HomeArticleData, HomeChangeNotifier>(
        createItem: (
          BuildContext context,
          int index,
          HomeArticleData data,
          HomeChangeNotifier homeChangeNotifier,
        ) {
          return _buildItem(context, index, data, homeChangeNotifier);
        },
      ),
    );
  }

  Widget _buildItem(
    BuildContext context,
    int index,
    HomeArticleData data,
    HomeChangeNotifier homeChangeNotifier,
  ) {
    return InkWell(
      //item的点击事件
      onTap: () {
        if (data.link != null) {
          CommonUtil.jumpToPageByPageName(
            context,
            AppRouter.web,
            {
              "loadUrl": data.link ?? "",
              "title": data.title,
            },
          );
        }
      },
      //item内容
      child: Container(
        color: Colors.white,
        padding: const EdgeInsets.all(10),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Row(
              children: <Widget>[
                Expanded(
                  child: Row(
                    children: <Widget>[
                      Image.asset(
                        'assets/mine/header.png',
                        width: 25,
                        height: 25,
                      ),
                      const Padding(padding: EdgeInsets.only(left: 10)),
                      Text(
                        data.author?.isEmpty == true ? "无名" : data.author!,
                        style: const TextStyle(fontSize: 14),
                      ),
                    ],
                  ),
                ),
              ],
            ),
            Padding(
              padding: const EdgeInsets.only(top: 10),
              child: Text(
                "${index + 1}. ${data.title}",
                textAlign: TextAlign.left,
                style: const TextStyle(fontSize: 15),
              ),
            ),
            Padding(
              padding: const EdgeInsets.only(top: 10),
              child: Row(
                children: <Widget>[
                  Expanded(
                    child: Text(
                      _getCategoryString(data),
                      style: const TextStyle(fontSize: 13),
                    ),
                  ),
                  Text(
                    data.niceDate ?? "",
                    style: const TextStyle(fontSize: 13),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  String _getCategoryString(HomeArticleData data) {
    String category = data.chapterName ?? "";
    String superCategory = data.superChapterName ?? "";
    return "分类：$superCategory/$category";
  }
}
